 
import { PositionHelper } from "utils/position/PositionHelper";
import { MineEntity } from "./MineEntity";
import { StationNetwork } from "GameLoop/Path/StationNetwork";
import { UtilsHelper } from "utils/position/UtilsHelper";

/**
 * 基础能源矿
 */
export class OutMineEntity<T extends IOutMineMemory =IOutMineMemory> extends MineEntity<T,IOutMineInitializer>
{
    protected workPos!:RoomPosition;
    protected basePos!:RoomPosition;
    protected max_energy!:number;
    public initAttributes(): void 
    {
        super.initAttributes();
        
        const nodememory = this.getNodeMemory();

        nodememory.type = "OutMineEntity";
   
        if(!nodememory.skey)
        {
            const obj = Game.getObjectById( this.GUID() as Id<Mineral>);
            if(obj)
            {
                nodememory.skey = UtilsHelper.serializeObject(obj!);
            }
            else 
            {
                this.log("error",`初始化 找不到矿石${this.GUID()}`)
              
            }
            
        }
        this._source = UtilsHelper.deSerializeObject(nodememory.skey)
        this.max_energy = UtilsHelper.isCenterRoom( this._source.pos.roomName)?4000:3000;
        this.basePos =this._source.pos;
        if(!nodememory.max)
        {
            nodememory.max = Math.min(this.BasePosition.getFreeSpace(1).length,3);
        }
        this.initPath(nodememory);
        
        this._pickupPos = this.BasePosition.getFreeSpace(1);  
        const container = this.getContainer() ;
        if(container  )
        {
            this.workPos = container.pos;
        }
        else
        {
            this.workPos = _.min(this._pickupPos,e=>e.distance(this._path[ this._path .length-1]));;
        }
    }
    public get region():string{
       
        return Memory.rooms[this._initializer.roomName].lord
        
    }
    public updateMemory(): void 
    {
        // 更新container
        if(!this.getNodeMemory().containerId ||(this.room && !Game.getObjectById(this.getNodeMemory().containerId as any)))
        {
            this.getNodeMemory().containerId =  PositionHelper.lookFor(LOOK_STRUCTURES, this.workPos).filter(e=>e.structureType == STRUCTURE_CONTAINER)[0]?.id as Id<StructureContainer>;
        }  
         // 更新维修目标
        const container = this.getContainer() ;
        if(container  )
        {
            if(container.hits<container.hitsMax*0.8)
            {
                this.getNodeMemory().repairObjects = [container.id];
            }
            
        }  
        else
        {
            if(this.room && this.lordRoom.controller.level>2)
            {
                this.WorkPosition.createConstructionSite(STRUCTURE_CONTAINER);
            }
            
        }
        // 更新site
        if(!this.getNodeMemory().sites || this.getNodeMemory().sites?.length ==0)
        {
            // 没有site
            if(this.room)
            {
                const sites = this.WorkPosition.lookForInRange(LOOK_CONSTRUCTION_SITES,3);
                if(sites && sites.length>0)
                {
                    this.getNodeMemory().sites = sites.map(e=>e.constructionSite.id);
                }
                else
                {
                    delete this.getNodeMemory().sites;
                }
            }
            
        }    

       
    }
    public  getTransferObject(): AnyStoreStructure |null
    {
        return null
    }
    public updateState(state:number)
    {
        super.updateState(state);
        
        Memory.rooms[this.roomName].state = state;
    }
    get BasePosition()
    {
        return  this.basePos;
    }
    
    get WorkPosition()
    {
        return this.workPos;
    }
   
    // 每Tick采集的能量数量
    public getEnergyHarvestSpeed():number
    {
        if(this.max_energy==4000)
        {
            // 中央房的采集速度
            return  4000/300* this.getHarvestEfficiency(4000)
        }
        if(this.lordRoom && this.lordRoom.controller.level>3)
        {
            return  10* this.getHarvestEfficiency(3000)
        }
        else
        {
            return  5* this.getHarvestEfficiency(1500)
        }
    }

    public getWorkBodyNum():number
    {
        if(this.max_energy==4000)
        {
            // 中央房的work数量
            return  10* this.getHarvestEfficiency(4000)
        }
        if(this.lordRoom && this.lordRoom.controller.level>3)
        {
            return  6* this.getHarvestEfficiency(3000)
        }
        else
        {
            return  3* this.getHarvestEfficiency(1500)
        }
    }
}
